#include "pch.h"
#include "SharedDesktopFrame.h"

SharedDesktopFrame::~SharedDesktopFrame() {}

std::unique_ptr<SharedDesktopFrame> SharedDesktopFrame::Wrap(
	std::unique_ptr<DesktopFrame> desktop_frame) {
	return std::unique_ptr<SharedDesktopFrame>(
		new SharedDesktopFrame(std::make_shared<Core>(std::move(desktop_frame))));
}

SharedDesktopFrame* SharedDesktopFrame::Wrap(DesktopFrame* desktop_frame) {
	return Wrap(std::unique_ptr<DesktopFrame>(desktop_frame)).release();
}

DesktopFrame* SharedDesktopFrame::GetUnderlyingFrame() {
	return core_->get();
}

bool SharedDesktopFrame::ShareFrameWith(const SharedDesktopFrame& other) const {
	return core_->get() == other.core_->get();
}

std::unique_ptr<SharedDesktopFrame> SharedDesktopFrame::Share() {
	std::unique_ptr<SharedDesktopFrame> result(new SharedDesktopFrame(core_));
	result->CopyFrameInfoFrom(*this);
	return result;
}

bool SharedDesktopFrame::IsShared() {
	return core_.use_count() > 1;
}

SharedDesktopFrame::SharedDesktopFrame(std::shared_ptr<Core> core)
	: DesktopFrame((*core)->size(),
	(*core)->stride(),
		(*core)->data(),
		(*core)->shared_memory()),
	core_(core) {
	CopyFrameInfoFrom(*(core_->get()));
}
